﻿using Hidistro.Core;
using Hidistro.Core.Enums;
using Hidistro.Entities;
using Hidistro.Entities.Commodities;
using Hidistro.SaleSystem.Catalog;
using System;
using System.Data;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Hidistro.UI.SaleSystem.Tags
{
    public class wnxd_DayTuiJian : WebControl
    {
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(this.RendHtml());
        }

        public string RendHtml()
        {
            StringBuilder builder1 = new StringBuilder();
            StringBuilder builder2 = new StringBuilder();
            if (this.type == 0)
            {
                builder2.AppendFormat("<div class=\"content6-js\">");
                builder1.AppendFormat("<div class=\"content7-js\">");
                int categoryId = int.Parse((HttpContext.Current.Handler as System.Web.UI.Page).Request.QueryString["CategoryId"]);
                DataTable brandCategories = this.GetProductList(categoryId, 8, this.maxNum);
                if ((brandCategories != null) && (brandCategories.Rows.Count > 0))
                {
                    int i = 0;
                    foreach (DataRow row in brandCategories.Rows)
                    {
                        i++;
                        builder2.AppendFormat("<div class=\"content6-box\" {0}>", i == brandCategories.Rows.Count ? "style=\"margin-right:0;\"" : "");
                        builder2.AppendFormat("<span>已出售<b>{0}</b>件</span>", row["SaleCounts"]);
                        builder2.AppendFormat("<p class='wnxd-text'><a href=\"{1}\"alt=\"{0}\" target=\"_blank\">{0}</a>", row["ProductName"], Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }));
                        if (row["ProductText"] != DBNull.Value)
                        {
                            builder2.AppendFormat("&nbsp;&nbsp;<a style=\"color:#F53864\" href=\"{0}\"alt=\"{1}\" target=\"_blank\">{1}</a>", Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }), row["ProductText"]);
                        }
                        builder2.AppendFormat("</p>");
                        if (row["RankPrice"] == DBNull.Value) row["RankPrice"] = row["MarketPrice"];
                        builder2.AppendFormat("<p><b>￥{0}</b><i>包邮</i></p>", Globals.FormatMoney((decimal)row["RankPrice"]));
                        builder2.Append("</div>");
                        string title = string.Empty;
                        string alt = string.Empty;
                        string keyword = row["Meta_Keywords"].ToString();
                        if (!string.IsNullOrEmpty(keyword))
                        {
                            string[] arr = keyword.Split('|');
                            if (arr.Length == 3)
                            {
                                title = arr[1];
                                alt = arr[2];
                            }
                        }
                        builder1.AppendFormat("<a href=\"{2}\" target=\"_blank\"><img data-original=\"{0}\" {1} title=\"{3}\" alt=\"{4}\"></a>", Globals.GetImgUrl(row["ThumbnailUrl310"].ToString()), i == brandCategories.Rows.Count ? "style=\"margin-right:0;\"" : "", Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }), title, alt);
                    }
                }
                builder1.AppendFormat("</div>");
                builder2.AppendFormat("</div>");
            }
            else if (this.type == 1)
            {
                builder1.AppendFormat("<div class=\"content8-left\">");
                builder2.AppendFormat("<div class=\"content8-right\">");
                int categoryId = int.Parse((HttpContext.Current.Handler as System.Web.UI.Page).Request.QueryString["CategoryId"]);
                DataTable brandCategories = this.GetProductList(categoryId, 4, this.maxNum);
                if ((brandCategories != null) && (brandCategories.Rows.Count > 0))
                {
                    bool first = true;
                    foreach (DataRow row in brandCategories.Rows)
                    {
                        string title = string.Empty;
                        string alt = string.Empty;
                        string keyword = row["Meta_Keywords"].ToString();
                        if (!string.IsNullOrEmpty(keyword))
                        {
                            string[] arr = keyword.Split('|');
                            if (arr.Length == 3)
                            {
                                title = arr[1];
                                alt = arr[2];
                            }
                        }
                        if (first)
                        {
                            first = false;
                            builder1.AppendFormat("<a href=\"{1}\" target=\"_blank\"><img data-original=\"{0}\" width=\"240px\" height=\"240px\" title=\"{2}\" alt=\"{3}\"></a>", Globals.GetImgUrl(row["ThumbnailUrl310"].ToString()), Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }), title, alt);
                            builder1.AppendFormat("<p><a href=\"{1}\"alt=\"{0}\" target=\"_blank\">{0}</a>", row["ProductName"], Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }));
                            if (row["ProductText"] != DBNull.Value) builder1.AppendFormat("&nbsp;&nbsp;<a style=\"color:#F53864\" href=\"{0}\"alt=\"{1}\" target=\"_blank\">{1}</a>", Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }), row["ProductText"]);
                            if (row["RankPrice"] == DBNull.Value) row["RankPrice"] = row["MarketPrice"];
                            builder1.AppendFormat("</p><p><b>￥{0}</b><i>包邮</i></p>", Globals.FormatMoney((decimal)row["RankPrice"]));
                        }
                        else
                        {
                            builder2.AppendFormat("<div class=\"pro8\">");
                            builder2.AppendFormat("<a href=\"{1}\" target=\"_blank\"><img data-original=\"{0}\" width=\"110px\" height=\"110px\" title=\"{2}\" alt=\"{3}\"></a>", Globals.GetImgUrl(row["ThumbnailUrl310"].ToString()), Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }), title, alt);
                            builder2.AppendFormat("<p class='wnxd-text'><a href=\"{1}\"alt=\"{0}\" target=\"_blank\">{0}</a>", row["ProductName"], Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }));
                            if (row["ProductText"] != DBNull.Value) builder2.AppendFormat("&nbsp;&nbsp;<a style=\"color:#F53864\" href=\"{0}\"alt=\"{1}\" target=\"_blank\">{1}</a>", Globals.GetSiteUrls().UrlData.FormatUrl("productDetails", new object[] { row["ProductId"], row["CategoryUrl"] }), row["ProductText"]);
                            if (row["RankPrice"] == DBNull.Value) row["RankPrice"] = row["MarketPrice"];
                            builder2.AppendFormat("</p><p><b>￥{0}</b><i>包邮</i></p>", Globals.FormatMoney((decimal)row["RankPrice"]));
                            builder2.AppendFormat("</div>");
                        }
                    }
                }
                builder1.AppendFormat("</div>");
                builder2.AppendFormat("</div>");
            }
            return builder1.ToString() + builder2.ToString();
        }
        private DataTable GetProductList(int CategoryId, int TagId, int MaxNum)
        {
            SubjectListQuery query = new SubjectListQuery
            {
                SortBy = "NewID()",
                CategoryIds = CategoryId.ToString(),
                TagId = TagId,
                MaxNum = MaxNum
            };
            return ProductBrowser.GetSubjectList(query);
        }

        public int maxNum { get; set; }
        public int type { get; set; }
    }
}
